import Vue from 'vue'
import Router from 'vue-router'
import Parent from 'components/home/home'
import Index from 'particals/index'
import store from 'store';

Vue.use(Router)

export default new Router({
  // mode: 'history',
  routes: [{
    path: '/',
    component: Index,
    children: [
      {
        path: '/',
        meta: {
          navShow: true
        },
        component: resolve => require(['particals/home/home'], resolve),
      }
    ]
  },{
    path: '/topics',
    component: Index,
    children: [
      {
        path: '/',
        meta: {
          navShow: true
        },
        component: resolve => require(['particals/topic/topic'], resolve),
      },{
        path: 'item_:id.html',
        meta: {
          navShow: false
        },
        component: resolve => require(['particals/item/artical'], resolve),
      }
    ]
  },{
    path: '/about',
    component: Index,
    children: [
      {
        path: '/',
        meta: {
          navShow: false
        },
        component: resolve => require(['particals/about/about'], resolve),
      }
    ]
  },{
    path: '/notes',
    component: Index,
    children: [
      {
        path: '/',
        meta: {
          navShow: true
        },
        component: resolve => require(['particals/notes/note'], resolve)
      },{
        path: ':id',
        name: 'record',
        component: resolve => require(['particals/records/records'], resolve)
      }
    ]
  },
  {
    path: '/login',
    component: Index,
    beforeEnter: requireLogin,
    children: [{
      path: '/',
      meta: {
        navShow: false
      },
      component: resolve => require(['particals/login/login'], resolve),
    }]
  },
  {
    path: '/register',
    component: Index,
    beforeEnter: requireLogin,
    children: [{
      path: '/',
      meta: {
        navShow: false
      },
      component: resolve => require(['particals/register/register'], resolve),
    }]
  },
  {
    path: '/user',
    component: Index,
    beforeEnter: requireAuth,
    children: [{
      path: 'modify',
      name: 'modify',
      meta: {
        navShow: false,
        auth: false
      },
      component: resolve => require(['particals/user/modify'], resolve),
    },{
      path: ':name',
      name: 'user',
      meta: {
        navShow: false,
        auth: true
      },
      component: resolve => require(['particals/user'], resolve),
    }]
  },
  {
    path: '/reset',
    component: Index,
    children: [{
      path: '/',
      name: 'resetPwd',
      meta: {
        navShow: false,
      },
      component: resolve => require(['particals/user/forgetPassword'], resolve),
    }]
  },
  {
    path: '/error',
    meta: {
      navShow: false
    },
    component: Index,
    children: [
    {
      path: ':status/:msg',
      name: 'error',
      meta: {
        navShow: false
      },
      component: resolve => require(['particals/errors/error'], resolve)
    }]
  }]
})

function requireLogin (to, from, next) {
  if(store.state.login) {
    return next('/')
  }else{
    return next();
  }
}

function requireAuth(to, from, next) {
  if(store.state.login || store.state.user.id) {
    return next()
  }else{
    return next('/login');
  }
}

function checkUser(to,from,next) {
  if(to.name === 'modify') {
    return next()
  }
  return next({name:'user',params:{name:'modify'}})
}
